<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset("layui/css/layui.css")}}"  media="all">
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'/admin/user/api/list'
            ,toolbar: '#toolbarDemo'
            ,title: '管理员数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'role_name', title:'角色名称', width:120, templet:function (d) {
                        return '<span class="layui-badge layui-bg-blue">'+d.role_name+'</span>';
                    }}
                ,{field:'username', title:'账号'}
                ,{field:'password', title:'密码'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type:2,
                        title:"新增管理员",
                        area:["50%","40%"],
                        content:'/admin/user/html/add'
                    });
                    break;

            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('该操作将永久删除此数据,是否确认?', function(index){
                    $.post('/admin/user/api/delete',{"id":data.id},function (d) {
                        if (d.code==200){
                            obj.del();
                        }else{
                            layer.msg("删除失败");
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    title:"修改密码",
                    content:'<input type="text" id="password" autocomplete="off" placeholder="请输入新密码" class="layui-input">',
                    btn:["保存","取消"],
                    yes:function () {
                        var password = $('#password').val();
                        $.post('/admin/user/api/password',{"id":data.id,"password":password},function (d) {
                            if (d.code==200){
                                layer.msg("密码更新成功");
                                setTimeout(function () {
                                    window.location.reload();
                                },500)
                            }else{
                                layer.msg("更新密码失败");
                            }
                        });
                    }
                });
            }
        });
    });
</script>

</body>
</html>